//
//  WBComposeTextView.swift
//  微博(swift)
//
//  Created by 汤米 on 16/4/24.
//  Copyright © 2016年 汤米. All rights reserved.
//

import UIKit

class WBComposeTextView: UITextView {
    
    
    
    // MARK: -  重写父类已有属性font
    override var font: UIFont? {
        
        didSet {
            
            placeHolderLabel.font = font
        }
    }
    
    // MARK: -  供外界设置占位文字
    var placeHolderString: String? {
        
        didSet {
            
            placeHolderLabel.text = placeHolderString
        }
    }

    // MARK: -  重写init方法
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        
        super.init(frame: frame, textContainer: textContainer)
        setUpUI()
    }
    
    
    // MARK: -  设置控件视图
    private func setUpUI() {
        
        addSubview(placeHolderLabel)
        
        placeHolderLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(5)
            make.top.equalTo(8)
            make.width.equalTo(kScreenW - 10)
        }
        
        //监听文字改变
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textViewTextDidChange", name: UITextViewTextDidChangeNotification, object: nil)
    }
    
    // MARK: -  监听textView的文字改变状态
    @objc private func textViewTextDidChange() {
        
        placeHolderLabel.hidden = self.hasText()
        
    }
    
    // MARK: -  懒加载控件
    private lazy var placeHolderLabel: UILabel = {
        
        let placeHolderLabel = UILabel()
        placeHolderLabel.numberOfLines = 0
        placeHolderLabel.textColor = UIColor.grayColor()
        placeHolderLabel.font = UIFont.systemFontOfSize(14)
        return placeHolderLabel
    }()

    // MARK: -  重写init方法时候必须要实现的方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  移除通知观察者
    deinit {
        
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}
